package com.diamond.smart.info.service.impl;

import com.diamond.smart.info.service.CityService;
import com.diamond.smart.info.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diamond.common.utils.PageUtils;
import com.diamond.common.utils.Query;

import com.diamond.smart.info.dao.DeviceDao;
import com.diamond.smart.info.entity.DeviceEntity;
import com.diamond.smart.info.service.DeviceService;
import org.springframework.util.StringUtils;


@Service("deviceService")
public class DeviceServiceImpl extends ServiceImpl<DeviceDao, DeviceEntity> implements DeviceService {

    @Autowired
    CityService cityService;

    @Autowired
    RegionService regionService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<DeviceEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.hasLength((String) params.get("key"))) {
            wrapper.eq("device_id", params.get("key")).or().eq("device_num", params.get("key"));
        }
        IPage<DeviceEntity> page = this.page(
                new Query<DeviceEntity>().getPage(params), wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 新增设备对应区域城市数量也要增长
     *
     * @param scDevice
     */
    @Override
    public void saveAll(DeviceEntity scDevice) {
        //保存设备信息
        boolean save = this.save(scDevice);
        if (save) {
            //修改城市设备数量
            cityService.updateDeviceCount(scDevice.getDeviceCity());
            //修改区域设备数量
            regionService.updateDeviceCount(scDevice.getDeviceRegion());
        }
    }

    /**
     * 多表修改，当设备状态修改为0弃用时修改所属区域城市数量
     *
     * @param scDevice
     */
    @Override
    public void updateAll(DeviceEntity scDevice) {
        boolean result = this.updateById(scDevice);
        if (result && scDevice.getDeviceStatus() == 0) {
            cityService.updateDeviceCountByUpdate(scDevice.getDeviceCity());
            regionService.updateDeviceCountByUpdate(scDevice.getDeviceRegion());
        }
    }

    /**
     * 根据设备类型查询设备信息
     *
     * @param typeId
     * @return
     */
    @Override
    public List<DeviceEntity> getDevicesByTypeId(Long typeId) {
        return this.list(new QueryWrapper<DeviceEntity>().eq("type_id", typeId));
    }

    /**
     * 根据设备编号查找信息
     *
     * @param deviceNum
     * @return
     */
    @Override
    public DeviceEntity getDeviceByDeviceNum(String deviceNum) {
        return this.getOne(new QueryWrapper<DeviceEntity>().eq("device_num", deviceNum).select("device_name"));
    }

    /**
     * 获取所有设备所在的详细地址
     * @return
     */
    @Override
    public List<String> getDeviceLocation() {
        List<DeviceEntity> entityList = this.list(new QueryWrapper<DeviceEntity>().select("device_addr"));
        return entityList.stream().map(DeviceEntity::getDeviceAddr).collect(Collectors.toList());
    }

}